//
//  PageHeaderView.swift
//  VO_Yuejie
//
//  Created by laichunhui on 15/12/1.
//  Copyright © 2015年 lch. All rights reserved.
//

import UIKit

class PageHeaderView: UICollectionReusableView {
    /// 主题配图
    lazy var coverPicture = UIImageView()
    
    /// 标题
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textAlignment = NSTextAlignment.Center
        titleLabel.textColor = UIColor.whiteColor()
        
        return titleLabel
    }()
    
    /// 作者
    lazy var authorIcon: UIButton = {
        let icon = UserIcon()
        
        icon.setTitle("主编：Bodhi", forState: .Normal)
        return icon
    }()
    
    /// 简介
    lazy var introLabel: UILabel = {
        let introLabel = UILabel()
        introLabel.numberOfLines = 0
        introLabel.textAlignment = NSTextAlignment.Center
        introLabel.font = UIFont.systemFontOfSize(12)
        introLabel.textColor = UIColor.colorWith(123, green: 129, blue: 132, alpha: 1)
        
        return introLabel
    }()

    
    var viewModel: TopicViewModel? {
        
        didSet {
            titleLabel.text = viewModel?.title
            
            if let image_url = viewModel?.image_url {
                coverPicture.vo_setImageWithURL(image_url, placeholderImage: UIImage(named: "sm_big_topic_default")!)
            }
            
            if let userIcon_url = viewModel?.userIconUrl {
                authorIcon.vo_setImageWithURL(userIcon_url, forState: .Normal, placeholderImage: UIImage(named: "sm_user_icon"), boderColor: UIColor.whiteColor(), borderWidth: 2)
            }
            
            introLabel.text = viewModel?.intro
            introLabel.sizeToFit()
        }
    }
    
    override func prepareForReuse() {
        
    }
    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.whiteColor()
        layoutInterface()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    /// 布局视图
    func layoutInterface() {
        
        self.addSubview(coverPicture)
        
        coverPicture.addSubview(titleLabel)
        
        self.addSubview(authorIcon)
        
        self.addSubview(introLabel)
        
        coverPicture.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self).offset(0)
            make.height.equalTo(ScreenWidth / 1.516)
        }
        
        titleLabel.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(30)
            make.width.equalTo(ScreenWidth)
            make.center.equalTo(coverPicture)
        }
        
        authorIcon.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(80)
            make.width.equalTo(52)
            make.centerX.equalTo(self)
            make.top.equalTo(coverPicture.snp_bottom).offset(-24)
        }
        
        introLabel.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self.width - 20)
            make.bottom.equalTo(self)
            make.top.equalTo(authorIcon.snp_bottom)
            make.leading.equalTo(self).offset(10)
        }
    }
    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        
//        
//    }
    
}
